package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import vo.UserVO;
	
	public class TestAdminUsers extends DefaultTestCase {
		
		private var administratorService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			administratorService = new RemoteObject();
			administratorService.destination = "amfphp";
			administratorService.source = "AdministratorService";
		}
		
		[After]
		public function tearDown():void {
			
			administratorService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST GET STATISTICS
		// ----------------------------------------------------------------
		[Test(async,order=1)]
		public function testGetStatistics():void {
			
			var argsVO:Object = {
				sessionID: "debug"
			};
			
			var token:AsyncToken = administratorService.getStatistics(argsVO.sessionID);
			runAsyncTest(token, argsVO, handleGetStatistics);
		}
		
		private function handleGetStatistics(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			
			Assert.assertNotNull(message.result.logins);
			Assert.assertNotNull(message.result.users);
		}
		
		// ----------------------------------------------------------------
		// TEST USER LIST
		// ----------------------------------------------------------------
		[Test(async,order=2)]
		public function testGetUserList():void {
			
			var argsVO:Object = {
				sessionID: "debug"
			};
			
			var token:AsyncToken = administratorService.getUserList(argsVO.sessionID);
			runAsyncTest(token, argsVO, handleGetUserList);
		}
		
		private function handleGetUserList(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result.length > 0));
		}
		
		// ----------------------------------------------------------------
		// TEST SMS USERS
		// ----------------------------------------------------------------
		[Test(async,order=3)]
		public function testSMSUsers():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				phoneNumbers: ["0827467226"],
				apiID: "test",
				userName: "test",
				password: "test",
				message: "This is a test message"
			};
			
			var token:AsyncToken = administratorService.smsUsers(argsVO.sessionID, argsVO.phoneNumbers, argsVO.apiID, argsVO.userName, argsVO.password, argsVO.message);
			runAsyncTest(token, argsVO, handleSMSUsers);
		}
		
		private function handleSMSUsers(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST EMAIL USERS
		// ----------------------------------------------------------------
		[Test(async,order=4)]
		public function testEmailUsers():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				emailAddresses: ["fdempers@gmail.com"],
				fromAddress: "test@test.com",
				heading: "Test Message",
				message: "This is a test message"
			};
			
			var token:AsyncToken = administratorService.emailUsers(argsVO.sessionID, argsVO.emailAddresses, argsVO.fromAddress, argsVO.heading, argsVO.message);
			runAsyncTest(token, argsVO, handleEmailUsers);
		}
		
		private function handleEmailUsers(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		
		// ----------------------------------------------------------------
		// TEST GOOD ADD USER
		// ----------------------------------------------------------------
		private static var addedUserUID:String;
		
		[Test(async,order=5)]
		public function testGoodAddUser():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				firstName: "Unit",
				lastName: "Test User 2",
				email: "test2@test.test",
				password: "password",
				phoneNumber: "0123456789",
				description: "User created by the admin unit testing process",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "Test User",
				institution: "Open Groups"
			};
				
			var token:AsyncToken = administratorService.addUser(
				argsVO.sessionID, argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleGoodAddUser);
		}
		
		private function handleGoodAddUser(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			addedUserUID = message.result as String;
		}
		
		// ----------------------------------------------------------------
		// TEST BAD ADD USER
		// ----------------------------------------------------------------
		[Test(async,order=6)]
		public function testBadAddUser():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				firstName: "Unit",
				lastName: "Test User 2",
				email: "admin@test.com",
				password: "password",
				phoneNumber: "0123456789",
				description: "User created by the unit testing process",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "Test User",
				institution: "Open Groups"
			};
			
			var token:AsyncToken = administratorService.addUser(
				argsVO.sessionID, argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleBadAddUser);
		}
		
		private function handleBadAddUser(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST GOOD EDIT USER
		// ----------------------------------------------------------------
		[Test(async,order=7)]
		public function testGoodEditUser():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001",
				firstName: "System",
				lastName: "Administrator",
				email: "admin@test.com",
				password: "password",
				phoneNumber: "0123456789",
				description: "123",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "",
				institution: ""
			};
			
			var token:AsyncToken = administratorService.editUser(
				argsVO.sessionID, argsVO.userUID,
				argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleGoodEditUser);
		}
		
		private function handleGoodEditUser(message:ResultEvent, passedData:Object):void {
			
			Assert.assertTrue(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST BAD EDIT USER
		// ----------------------------------------------------------------
		[Test(async,order=8)]
		public function testBadEditUser():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user002",
				firstName: "System",
				lastName: "Administrator",
				email: "admin@test.com",
				password: "password",
				phoneNumber: "0123456789",
				description: "123",
				gender: "M",
				dateOfBirth: "2009-06-12",
				location: "",
				occupation: "",
				institution: ""
			};
			
			var token:AsyncToken = administratorService.editUser(
				argsVO.sessionID, argsVO.userUID,
				argsVO.firstName, argsVO.lastName,
				argsVO.email, argsVO.password,
				argsVO.phoneNumber, argsVO.description,
				argsVO.gender,  argsVO.dateOfBirth,
				argsVO.location, argsVO.occupation, argsVO.institution
			);
			runAsyncTest(token, argsVO, handleBadEditUser);
		}
		
		private function handleBadEditUser(message:ResultEvent, passedData:Object):void {
			
			Assert.assertFalse(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST DELETE USER
		// ----------------------------------------------------------------
		[Test(async,order=9)]
		public function testDeleteUser():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: addedUserUID
			};
			
			var token:AsyncToken = administratorService.deleteUser(argsVO.sessionID, argsVO.userUID);
			runAsyncTest(token, argsVO, handleDeleteUser);
		}
		
		private function handleDeleteUser(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertEquals(addedUserUID, message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST GET USER DETAILS
		// ----------------------------------------------------------------
		[Test(async,order=10)]
		public function testGetUserDetails():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				userUID: "user001"
			};
			
			var token:AsyncToken = administratorService.getUserDetails(argsVO.sessionID, argsVO.userUID);
			runAsyncTest(token, argsVO, handleGetUserDetails);
		}
		
		private function handleGetUserDetails(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result is UserVO));
		}
	}
}